package SeparateAnalysis;

/**
 * Created by IntelliJ IDEA.
 * User: Mike
 * Date: 11/11/11
 * Time: 2:12 PM
 * To change this template use File | Settings | File Templates.
 */

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.Rotation;

import java.awt.*;
import java.util.List;


public class ChartPie extends ApplicationFrame {

    private JFreeChart chart;

    public JFreeChart getChart() {
        final PieDataset dataset = createSampleDataset();
        this.chart = createChart(dataset);
        return chart;
    }

    public JFreeChart getChart2() {
        final PieDataset dataset = createSampleDataset2();
        this.chart = createChart(dataset);
        return chart;
    }


    public ChartPie(final String title) {

        super(title);

        final PieDataset dataset = createSampleDataset();
        this.chart = createChart(dataset);

        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);

    }


    public PieDataset createSampleDataset() {

        final DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Heads", new Double(25));
        result.setValue("Hearts", new Double(35));
        result.setValue("Tails", new Double(40));
        return result;
    }

    public PieDataset createSampleDataset2() {

        final DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Heads", new Double(30));
        result.setValue("Hearts", new Double(40));
        result.setValue("Tails", new Double(30));
        return result;

    }


    private JFreeChart createChart(final PieDataset dataset) {

        this.chart = ChartFactory.createPieChart3D(
                "Run",  // cBar title
                dataset,                // data
                true,                   // include legend
                true,
                false
        );

        final PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(90);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        plot.setNoDataMessage("No data to display");

        plot.setBackgroundPaint(Color.white);
        plot.setDarkerSides(true);
        plot.setLabelGenerator(null);


        // Specify the colors here
        Color[] colors = {Color.blue, Color.yellow, Color.green};
        PieRenderer renderer = new PieRenderer(colors);
        renderer.setColor(plot, dataset);

        return chart;
    }

    public static void main(final String[] args) {

        final ChartPie demo = new ChartPie("Pie Chart 3D Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }


    public static class PieRenderer {
        private Color[] color;

        public PieRenderer(Color[] color) {
            this.color = color;
        }

        public void setColor(PiePlot3D plot, PieDataset dataset) {
            List<Comparable> keys = dataset.getKeys();
            int aInt;

            for (int i = 0; i < keys.size(); i++) {
                aInt = i % this.color.length;
                plot.setSectionPaint(keys.get(i), this.color[aInt]);
            }
            plot.setSectionPaint(keys.get(0), Color.yellow);
            plot.setSectionPaint(keys.get(1), Color.blue.darker().darker());
            plot.setSectionPaint(keys.get(2), Color.green.darker().darker());
        }
    }
}